C++ is a very powerful language and can also sometimes be very confusing. This text will breakdown a C++ program into simple and plain english so you can see the elements of a normal C++ program and be comfortable with programming. This thing will suck to anyone who knows how to code, sucks to me. But maybe not to people trying to code.
Okay here's the source code for a very simple c++ app.
//this program will determine which number is the larger of the two
//Written by hit
#include <iostream.h>
void main()
{
float num1,
num2;
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
if(num1>num2)
cout<<"the larger number is the first "<< num1;
if (num2>num1)
cout<<"the larger number is the second "<< num2;
if (num1==num2)
cout<<"bother numbers are the same";
}
Okay,let me break this down line by line.
// is a comment and is ignored by your c++ compiler. Another way to write a comment is /* blah blah*/ - this too is ignored by the compiler. Comments are used to explain to you or others of what the program or that line of code does.
#include <iostream.h> is a preprocessor directive. This #include tells the compiler to include the declarations for the standard i/o (input,output) stream header iostream.h Without this header you will be getting errors saying you never declared num1, etc.
Now to the next line. Every C++ program needs a main() function. You can use main(), int main(), or void main() i don't care. But in this program we used void because no value is returned by the program to the os.blah blah blah. I'll try to keep this simple. Anyway, just use void for now.
We use the brackets { } to group things called functions. They mark the beginning and end of a function.
The float is a data type and num1 is a variable. I could've used the data type int but i feel safer using float. Float is used when the variable is a small whole number. A made-up symbol for a number or value is a variable. Like cars could be a variable for number of cars in the lot,etc.etc,etc,etc,etc,etc,err .What the line is doing is declaring that the variable is being created. This is called declaration. Also note the comma. The next line is also delcaring a variable, not the semi-colon. Before you jump to type something else,always end it with a semi-colon. (That's the best way to explain it.)
Aight, we got that done. This is how i explain it to my classmates, may not be the correct way but you understand it...
cout<<"Enter first number: ";
Okay, cout<< is sorta saying i want you "computer" to put OUT on the screen this, "Enter first number: " (remember the dreaded semi-colon);
cin>>num1;
this is saying okay, when the user INputs the information, store this in the variable num1. Remember the ; to end the line
cout<<"Enter second number: ";
cin>>num2;
this is doing the same thing but outputing and inputing different information.
if(num1>num2)
cout<<"the larger number is the first "<< num1;
"if" is a branch which is saying okay if the variable num1 is equal to num2 then do this, get me?
So this line is saying if the variable num1 is greater the num2 then print on the screen "the larger number is the first"
<< num1; is that variable/number. Confusing? Hope not.
if (num2>num1)
cout<<"the larger number is the second "<< num2;
This is saying if num2 is greater than num1 then print "blah blah blah"and show the num2(whatever the user inputted for num2)
if (num1==num2)
cout<<"bother numbers are the same";
OKay, this says if num1 is equal to num2 then put the screen both numbers are the same. Why the two equal signs? I dunno. One equal sign works for me but keep on safe side.
And as always end the function with a }
Okay let's try to create our very own c++ program. This is in Borland C++ so it should compile on win and mac, If on a mac use Codewarrior or Symantec. If you're on a pc use Turbo C++.
We want our program when compiled to ask the user for two numbers. Then it will spit out the sum, difference, product and quotient. How will we do this? Simple. First we must make a chart of how it will work. Like this..
Inputs number 1
Inputs number 2
Outputs sum of num1 and num2
Outputs difference of num1 and num2
Outputs product of num1 and num2
Outputs quotient of num1 and num2
We know how the program will work, we know what we want, and now we must figure out how to write the program. This is be very simple because you now know elements of a c++ program.
The program begins with
#include <iostream.h>
void main()
{
Now we must figure out what variables we gotta have. Look at the chart. We need the variables num1, num2, sum, difference, product, and quotient!
So lets add this to the program
float num1,
num2,
sum,
difference,
product,
quotient;
Now remember the chart? We want the user to input numbers 1 and 2. So...
cout<<"Enter first number: "<<endl;
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
Now we have to enter some REALLY REALLY simple math formulas to get the sum, diff, etc.
sum=num1+num2;
HAHAHA Simple right? Lets keep going then...
difference=num1-num1;
product=num1*num2;
quotient=num1/num2;
Pow that's that. Now take a look at our chart, we have to output these answers. Okay lets do this
cout<<"The sum is: "<< sum<<endl; //btw endl; means end line.sorta saying skip to next line now
cout<<"The difference is: "<< difference<<endl;
cout<<"The product is: "<< product<<endl;
cout<<"The quotient is" << quotient<<endl;
And as always end the source with a
}
Lets take a look at our complete source
#include <iostream.h>
void main()
{
float num1,
num2,
sum,
difference,
product,
quotient;
cout<<"Enter first number: "<<endl;
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
sum=num1+num2;
difference=num1-num1;
product=num1*num2;
quotient=num1/num2;
cout<<"The sum is: "<< sum<<endl;
cout<<"The difference is: "<< difference<<endl;
cout<<"The product is: "<< product<<endl;
cout<<"The quotient is" << quotient<<endl;
}
I hope that was pretty easy. You should by now know how to write a simple program in c++ and or figure out what a program does just by looking at the src. If so then this text really worked, If not mail jambie and i'll try to put out another one (better written) for iss 11 or in midnight_raid or something.